package ij.umasuo;

import ij.IJ;
import ij.hkust.gui.laserCtr.STORM.CameraLabel;
import ij.hkust.gui.laserCtr.STORM.OneFrame;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * user settings that will be saved into a file. each time we change the settings, we will save the settings into the setting configuration file when the window is closing.
 * @author liuquan
 *
 */
public class MyUserSettings {
	public static String fileName;
	
	//+++++++EPI settings
//	public static int epiLaserCount;//the count of laser
	public static List<String> epiLasers = new ArrayList<String>();//the lasers 
	public static List<String> epiLaserInt = new ArrayList<String>();//the intensity of each laser
	
	//++++++++DSTORM settings these settings may be copied from epi
//	public static int dsLaserCount;//the count of laser
	public static List<String> dstLasers = new ArrayList<String>();//the lasers 
	public static List<String> dstLaserInt = new ArrayList<String>();//the intensity of each laser
	
	//++++++STORM settings
//	public static int stLaserCount;//laser count
	public static List<String[]> stLasers = new ArrayList<String[]>();//laser'ID list
	public static List<String> stLaserPower = new ArrayList<String>();//laser's power
	public static List<String> stCamera = new ArrayList<String>();
	public static List<String> stChannel = new ArrayList<String>();
	
	private static Properties settings = new Properties();//the properties to load and save the settings
	
	/**
	 * load settings from the file.here we can know the home dir of the software
	 * @param file
	 */
	public static void loadSettings(String file){
		fileName = file;
		try {
			//load the setting file, and 
			settings.load(new FileInputStream(fileName));
			
			//TODO load other settings
			
			
			//########read EPI settings
			int epiCount;
			if(settings.getProperty("epi_count") == null){
				epiCount = 0;
			}else{
				epiCount = Integer.parseInt(settings.getProperty("epi_count"));
			}
			 
			for(int i=0;i<epiCount;i++){
				epiLasers.add(settings.getProperty("epi_laser_" + i));
				epiLaserInt.add(settings.getProperty("epi_int_" + i));
			}
			//########read DSTORM settings
			int dstCount;
			if(settings.getProperty("dst_count") == null){
				dstCount = 0;
			}else{
				dstCount = Integer.parseInt(settings.getProperty("dst_count"));
			}
			
			for(int i=0;i<dstCount;i++){
				dstLasers.add(settings.getProperty("dst_laser_" + i));
				dstLaserInt.add(settings.getProperty("dst_int_" + i));
			}
			
			//###########read storm settings###################
			int stCount;
			if(settings.getProperty("storm_count") == null){
				stCount = 0;
			}else{
				stCount = Integer.parseInt(settings.getProperty("storm_count"));
			}
			//read lasers and frames
			for(int i=0;i<stCount;i++){
				String[] tmpLaser = settings.getProperty("st_laser_" + i).split(",");
				String tmpCamera = settings.getProperty("st_camera_" + i);
				String tmpChannel = settings.getProperty("st_channel_" + i);
				stLasers.add(tmpLaser);
				stCamera.add(tmpCamera);
				stChannel.add(tmpChannel);				
			}
			for(int i=0;i<6;i++){
				String tmpLaserPower = settings.getProperty("st_power_" + i);
				stLaserPower.add(tmpLaserPower);
			}
			//##################END of storm setting#############
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * write all the settings into the settings file
	 */
	public static void saveSettings(){
		Properties saver = new Properties();
		
		//add the new settings to the properties
		
		//#########the EPI settings#############
		saver.put("epi_count", epiLasers.size() + "");
		for(int i=0;i<epiLasers.size();i++){
			saver.put("epi_laser_" + i,epiLasers.get(i));
			saver.put("epi_int_" + i, epiLaserInt.get(i));
		}
		
		//#########the DAOSTORM settings#############
		saver.put("dst_count", dstLasers.size() + "");
		for (int i = 0; i < dstLasers.size(); i++) {
			saver.put("dst_laser_" + i, dstLasers.get(i));
			saver.put("dst_int_" + i, dstLaserInt.get(i));
		}
		
		//########## the storm settings###########
		saver.put("storm_count", stCamera.size() + "");
		for(int i=0;i<stCamera.size();i++){
			String tmpLasers = "";
			for(int j=0;j<stLasers.get(i).length-1;j++){
				tmpLasers += stLasers.get(i)[j];
				tmpLasers += ",";
			}
			tmpLasers += stLasers.get(i)[stLasers.get(i).length-1];
			
			saver.put("st_laser_" + i, tmpLasers);
			saver.put("st_camera_"+i,stCamera.get(i));
			saver.put("st_channel_"+i,stChannel.get(i));
		}
		for(int i=0;i<6 && stCamera.size()!=0;i++){
			saver.put("st_power_" + i, stLaserPower.get(i));
		}
		
		//save to the file
		try {
			OutputStream fos = new FileOutputStream(fileName);
			saver.store(fos, "save the user settings");
		} catch (Exception e) {
			e.printStackTrace();
			IJ.error("User Setting Error" ,e.toString());
		}
	}
	
	/**
	 * 
	 * @param laser
	 * @param power
	 */
	public static void addEpiCtr(String laser,String power){
		if(laser != null){
			epiLasers.add(laser);
			epiLaserInt.add(power);
//			epiLaserCount++;
		}
	}
	/**
	 * 
	 * @param laser
	 * @param power
	 */
	public static void addDstCtr(String laser,String power){
		if(laser == null || power == null){
			//TODO: do something
		}else{
			dstLasers.add(laser);
			dstLaserInt.add(power);
		}
	}

	/**
	 * add one frame to storm use
	 * @param laser
	 * @param frames
	 */
	public static void addStormCtr(String[] laser,String laserPower, String camera,String channel){
		if(laser != null && camera != null && channel != null){
			stLasers.add(laser);
			stLaserPower.add(laserPower);
			stCamera.add(camera);
			stChannel.add(channel);
		}
	}
	/**
	 * add one frame to storm use
	 * @param laser
	 * @param frames
	 */
	public static void addStormCtr(String[] laser, String camera,String channel){
		if(laser != null && camera != null && channel != null){
			stLasers.add(laser);
			stCamera.add(camera);
			stChannel.add(channel);
		}
	}
	
}
